<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板2</title>
    <link rel="stylesheet" href="./css/bootstrap.css">
</head>

<body>
    <div id="app">
        <cpns @senddata='recevicData'></cpns>
        <div class="container">
            <h1>留言板</h1>
            <ul class="list-group">
                <li class="list-group-item" v-for="item in msgs" :key='item.id'>
                    <p>
                        留言人：{{item.author}}
                    </p>
                    <p>
                        留言内容：{{item.centent}}
                    </p>
                </li>
            </ul>
        </div>

    </div>

    <template id="cpn">
        <div class="container">
            <form>
                <div class="form-group">
                    <label for="author">留言人：</label>
                    <input v-model='author' type="text" class="form-control" id="author" placeholder="留言人">
                </div>
                <div class="form-group">
                    <label for="centent">留言内容：</label>
                    <input v-model='centent' type="text" class="form-control" id="centent" placeholder="留言内容">
                </div>
                <button type="button" class="btn btn-default" @click.prevent='send'>发布</button>
            </form>
        </div>
    </template>
</body>
<script src='../js/vue.js'></script>
<script>
    Vue.component('cpns', {
        template: '#cpn',
        data() {
            return {
                author: '',
                centent: ''
            }
        },
        methods: {
            send() {
                this.$emit('senddata', this.author, this.centent)
                this.author=''
                this.centent=''
            }
        }
    })

    let app = new Vue({
        el: '#app',
        data: {
            msg: 'hello vue.js',
            msgs: [{
                id: 1,
                author: '张三',
                centent: 'hello'
            }]
        },
        methods: {
            recevicData(author, centent) {
                this.msgs.unshift({
                    id: this.msgs.length + 1,
                    author: author,
                    centent: centent
                })
            }
        }
    })
</script>

</html>